package com.agh.szepczynski.magisterka.service.controllers;

import java.util.Calendar;
import java.util.Date;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.map.ObjectMapper;

import com.agh.szepczynski.magisterka.service.algorithms.AlgorithmBars;
import com.agh.szepczynski.magisterka.service.algorithms.AlgorithmNumbers;
import com.agh.szepczynski.magisterka.service.algorithms.AlgorithmRace;
import com.agh.szepczynski.magisterka.service.algorithms.AlgorithmRoulette;
import com.agh.szepczynski.magisterka.service.algorithms.AlgorithmTournament;
import com.agh.szepczynski.magisterka.service.algorithms.IContestAlghoritm;
import com.agh.szepczynski.magisterka.service.beans.dto.EventAlgorithm;
import com.agh.szepczynski.magisterka.service.beans.dto.FacebookCreateEventRequest;
import com.agh.szepczynski.magisterka.service.beans.dto.FacebookCreateEventResponse;
import com.agh.szepczynski.magisterka.service.conf.SpringContainer;
import com.agh.szepczynski.magisterka.service.events.Event;
import com.agh.szepczynski.magisterka.service.events.EventEngine;

// Plain old Java Object it does not extend as class or implements 
// an interface

// The class registers its methods for the HTTP GET request using the @GET annotation. 
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML. 

// The browser requests per default the HTML MIME type.

//Sets the path to base URL + /hello

//the url is http://localhost:8080/magisterka-service/rest/action

@Path("/facebook/create_event")
public class FacebookCreateEventController {
protected static final Log logger = LogFactory
		.getLog(FacebookCreateEventController.class);
	EventEngine engine = SpringContainer.getBean(EventEngine.class);

	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public String createEvent(String message) throws Exception {
		logger.debug(message);
		ObjectMapper mapper = new ObjectMapper();
		FacebookCreateEventRequest request = null;
		request = mapper.readValue(message, FacebookCreateEventRequest.class);
		FacebookCreateEventResponse response = createFacebookEvent(request);
		response.setStatus("OK");
		String result = mapper.writeValueAsString(response);
		logger.debug(result);
		return result;
	}

	private FacebookCreateEventResponse createFacebookEvent(
			FacebookCreateEventRequest request) throws Exception {
		engine.clearEvents();
		Event event = createEvent(request);
		String token = createToken(request.getPostId());

		engine.addEvent(token, event);
		FacebookCreateEventResponse response = new FacebookCreateEventResponse();
		response.setStatus("OK");
		response.setToken(token);
		return response;
	}

	private String createToken(String postUrl) {
		return "token_lol";
	}

	private Event createEvent(FacebookCreateEventRequest request)
			throws Exception {
		IContestAlghoritm algorithm = null;
		
		switch (EventAlgorithm.valueOf(request.getAlgorithm())) {
		case ALGORITHM_RACE:
			algorithm = new AlgorithmRace();
			break;
		case ALGORITHM_BARS:
			algorithm = new AlgorithmBars();
			break;
		case ALGORITHM_NUMBERS:
			algorithm = new AlgorithmNumbers();
			break;
		case ALGORITHM_ROULETTE:
			algorithm = new AlgorithmRoulette();
			break;
		case ALGORITHM_TOURNAMENT:
			algorithm = new AlgorithmTournament(); 
			break;
		default:
			throw new Exception("No algorithm selected");
		}

		Event event = new Event(algorithm);
		event.setName("Thomas' event");
		event.setCreator("Tommy");
		Date dateCreated = new Date();
		event.setDateCreated(dateCreated);

		Calendar cal = Calendar.getInstance();
		cal.setTime(dateCreated);
		cal.add(Calendar.MINUTE, 15); // todo fire the event in 15 minutes
		event.setDateToBeFired(cal.getTime());
		return event;
	}
}